// import { routerRedux } from 'dva/router';
import { message } from 'antd';
import {
  getHrdtData,
  getHmldData,
  getHlbtData,
  getHldtData,
  getHomeMeidaList,
  addSmtpEntry,
} from '@/services/api';
import {
  getFSAbnormal,
  getFDAbnormal,
  getQpzs,
  getJyyjAPI,
  getHgyjAPI,
  getSzyjAPI,
  getYqyjAPI,
} from '@/services/warning';
import initChart from '@/utils/initChart';
import {
  getHomeRadarChart,
  getHomeDoubleLineChart,
  getHomeLineBarChart,
  getHomeLineChart,
} from '@/utils/echarts-figures.js';

export default {
  namespace: 'home',

  state: {
    data: {},
    loading: true,
    radarChart: {
      complex: {
        score: '0',
        industry_rank: '0%',
        score_flag: '-',
      },
    },
    lineBar: {},
    FSAbnormal: {},
    FinancialDataAbnormal: {},
    exoticIndexAbnormal: {},
    videoSelcet: [],
    JyyjData: {}, // 经营预警
    HgyjData: {}, // 合规预警
    SzyjData: {}, // 市值预警
    YqyjData: {}, // 舆情预警
    tipX: 0,
    tipY: 0,
  },

  effects: {
    // 经营预警
    *getJyyj({ payload }, { call, put }) {
      try {
        const res = yield call(getJyyjAPI, payload);
        console.log('经营预警[数据]:', res.data);
        if (res) {
          yield put({ type: 'getJyyjData', payload: res.data });
        }
      } catch (e) {
        yield put({ type: 'getJyyjData', payload: {} });
        // console.log('合规预警:', e);
      }
    },

    // 合规预警
    *getHgyj({ payload }, { call, put }) {
      try {
        const res = yield call(getHgyjAPI, payload);
        // console.log('合规预警[数据]:', res.data);
        if (res) {
          yield put({ type: 'getHgyjData', payload: res.data });
        }
      } catch (e) {
        yield put({ type: 'getHgyjData', payload: {} });
        // console.log('合规预警:', e);
      }
    },

    // 市值预警
    // *getSzyj({ payload }, { call, put }) {
    //   try {
    //     const res = yield call(getSzyjAPI, payload);
    //     // console.log('合规预警[数据]:', res.data);
    //     if(res){ yield put({ type: 'getSzyjData', payload: res.data }) }
    //   } catch (e) {
    //     yield put({ type: 'getSzyjData', payload: {} })
    //     // console.log('合规预警:', e);
    //   }
    // },

    // 舆情预警
    *getYqyj({ payload }, { call, put }) {
      try {
        const res = yield call(getYqyjAPI, payload);
        // console.log('舆情预警[数据]:', res.data);
        if (res) {
          yield put({ type: 'getYqyjData', payload: res.data });
        }
      } catch (e) {
        yield put({ type: 'getYqyjData', payload: {} });
        // console.log('合规预警:', e);
      }
    },

    // 财务科目异常变动
    *getFsan({ payload }, { call, put }) {
      try {
        const res = yield call(getFSAbnormal, payload);
        // console.log('财务科目异常变动[数据]:', res.data);
        yield put({ type: 'getFSAbnormalData', payload: res.data });
      } catch (e) {
        console.log('财务科目异常变动:', e);
      }
    },

    // 财务数据异常变动
    *getFdaw({ payload }, { call, put }) {
      try {
        const res = yield call(getFDAbnormal, payload);
        // console.log('财务数据异常变动[数据]:', res);
        yield put({ type: 'getFDAWData', payload: res.data.data_list });
      } catch (e) {
        console.log('财务数据异常变动:', e);
      }
    },

    // 奇葩指数异常
    *getEiaw({ payload }, { call, put }) {
      try {
        const res = yield call(getQpzs, payload);
        // console.log('奇葩指数异常变动[数据]:', res);
        yield put({ type: 'getEiawData', payload: res.data.data_list });
      } catch (e) {
        console.log('财务数据异常变动:', e);
      }
    },

    // 首页-企业发展评估
    *getHrdt({ chartID, payload }, { call, put }) {
      try {
        const res = yield call(getHrdtData, payload);
        console.log('企业发展评估(res) >>> ', res);
        sessionStorage.setItem('radarContent', JSON.stringify(res.data));
        yield put({ type: 'getHrdtListData', payload: res.data });

        if (res.code === 0) {
          const options = getHomeRadarChart(res.data);
          initChart(options, chartID);
        }
      } catch (e) {
        console.log('企业发展评估:', e);
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },

    // 首页-舆情走势图（近一周）
    *getHmld({ chartID, payload }, { call, put }) {
      try {
        const res = yield call(getHmldData, payload);
        // console.log("舆情走势图 >>> ", res.data.data_info);
        yield put({ type: 'getHmldListData', payload: res.data.data_info });
        if (res.code === 0) {
          // console.log('舆情走势图:',res.data)
          const options = getHomeDoubleLineChart(res.data.data_info);
          // console.log('生成的选项：', options);
          initChart(options, chartID);
        } else {
          // message.error('暂无数据');
        }
      } catch (e) {
        console.log('舆情走势图:', e);
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },

    // 首页-E互动
    *getHlbt({ chartID, payload }, { call, put }) {
      try {
        const res = yield call(getHlbtData, payload);
        // console.log('E互动 >>> ', res);
        yield put({ type: 'getHlbtListData', payload: res.data });
        if (res.code === 0) {
          const options = getHomeLineBarChart(res.data);
          // console.log('生成的选项：', options);
          initChart(options, chartID);
        } else {
          // message.error('暂无数据');
        }
      } catch (e) {
        console.log('舆情走势图:', e);
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },

    // 首页-市值变动图
    *getHldt({ payload }, { call, put }) {
      try {
        const res = yield call(getHldtData, payload);
        yield put({ type: 'getHldtListData', payload: res.data });
        // console.log("市值变动图 >>> ", res);
        if (res.code === 0) {
          const options = getHomeLineChart(res.data.data);

          // console.log('生成的选项：', options);
          initChart(options, payload.chartID);
        } else {
          // message.error('暂无数据');
        }
      } catch (e) {
        console.log('舆情走势图:', e);
        // message.error('获取数据失败，请稍后再试');
        // yield put({type: "USER_FETCH_FAILED", message: e.message});
      }
    },

    // 首页-新闻视频 getHomeMeidaList
    *getHmlp({ payload }, { call, put }) {
      try {
        const res = yield call(getHomeMeidaList, payload);
        // console.log("视频新闻 >>> ", res.data.data_list);
        yield put({ type: 'getHmlpListData', payload: res.data.data_list });
      } catch (e) {
        console.log('舆情走势图:', e);
      }
    },
  },
  reducers: {
    // 合规预警
    getJyyjData(state, { payload }) {
      return {
        ...state,
        JyyjData: { ...payload },
      };
    },
    // 合规预警
    getHgyjData(state, { payload }) {
      return {
        ...state,
        HgyjData: { ...payload },
      };
    },
    // 市值预警
    getSzyjData(state, { payload }) {
      return {
        ...state,
        SzyjData: { ...payload },
      };
    },
    // 舆情预警
    getYqyjData(state, { payload }) {
      return {
        ...state,
        YqyjData: { ...payload },
      };
    },
    getFSAbnormalData(state, { payload }) {
      return {
        ...state,
        FSAbnormal: { ...payload },
      };
    },
    getFDAWData(state, { payload }) {
      return {
        ...state,
        FinancialDataAbnormal: { ...payload },
      };
    },
    getEiawData(state, { payload }) {
      return {
        ...state,
        exoticIndexAbnormal: { ...payload },
      };
    },

    getHrdtListData(state, { payload }) {
      // console.log("radarChart-payload",state)
      return {
        ...state,
        radarChart: { ...payload },
      };
    },

    getHmldListData(state, { payload }) {
      return {
        ...state,
        doubleLine: { ...payload },
      };
    },

    getHlbtListData(state, { payload }) {
      return {
        ...state,
        lineBar: { ...payload },
      };
    },

    getHldtListData(state, { payload }) {
      return {
        ...state,
        line: { ...payload },
      };
    },

    getHmlpListData(state, { payload }) {
      return {
        ...state,
        videoSelcet: { ...payload },
      };
    },
  },
};
